package com.wx.date;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SDFTest {
    /**
     * 定义一个全局的 SimpleDateFormat
     */
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");

    private static ThreadLocal<SimpleDateFormat> simpleDateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    /**
     * 非线程安全的
     *
     * @throws InterruptedException 中断异常
     */
    @Test
    public  void  testSdf() throws InterruptedException {



        ExecutorService pool = Executors.newFixedThreadPool(10, new ThreadFactoryBuilder().setNameFormat("date-format-%d").build());
        /**
         * 定义一个 CountDownLatch，保证所有子线程执行完之后主线程再执行
         */
        CountDownLatch latch = new CountDownLatch(100);

        Set<String> dates = Collections.synchronizedSet(new HashSet<String>());
        for (int i = 0; i < 100; i++) {
            // 获取当前时间
            Calendar calendar = Calendar.getInstance();
            int finalI = i;
            pool.execute(() -> {
                synchronized (simpleDateFormat) {
                    // 时间增加
                    calendar.add(Calendar.DATE, finalI);
                    // 通过 simpleDateFormat 把时间转换成字符串
                    String dateString = simpleDateFormat.format(calendar.getTime());
                    dates.add(dateString);
                    latch.countDown();
                }
            });
        }
        latch.await();
        System.out.println(dates.size());
    }


    /**
     * 线程安全的
     *
     * @throws InterruptedException 中断异常
     */
    @Test
    public  void  testSynSdf() throws InterruptedException {

        ExecutorService pool = Executors.newFixedThreadPool(10, new ThreadFactoryBuilder().setNameFormat("date-format-%d").build());
        /**
         * 定义一个 CountDownLatch，保证所有子线程执行完之后主线程再执行
         */
        CountDownLatch latch = new CountDownLatch(100);

        Set<String> dates = Collections.synchronizedSet(new HashSet<String>());
        for (int i = 0; i < 100; i++) {
            // 获取当前时间
            Calendar calendar = Calendar.getInstance();
            int finalI = i;
            pool.execute(() -> {
                // 时间增加
                calendar.add(Calendar.DATE, finalI);
                // 通过 simpleDateFormat 把时间转换成字符串
                String dateString = simpleDateFormatThreadLocal.get().format(calendar.getTime());
                //System.out.println(Thread.currentThread().getName()+"--"+dateString+"--"+finalI);
                dates.add(dateString);
                latch.countDown();
            });
        }
        latch.await();
        System.out.println(dates.size());
    }


    /**
     * java 安全的日期转化
     *
     * @throws InterruptedException 中断异常
     */
    @Test
    public  void  testJ8Sdf()  {

        // 解析日期
        String dateStr= "2016年10月25日";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        LocalDate date= LocalDate.parse(dateStr, formatter);
        System.out.println(date);
        // 日期转换为字符串
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm a");
        String nowStr = now .format(format);
        System.out.println(nowStr);
    }
}

